'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function ($scope, $rootScope, $state, $timeout) {
            var title = "";

            // 提交数据
            $scope.submit = function(){
                $scope.loading = true;

                if(!$('#userId').val()){
                    $scope.alertModal({'content':'请选择管理用户'});
                    return;
                }
                
                if(!$('#channelNo').val()){
                    $scope.alertModal({'content':'请选择频道'});
                    return;
                }
                
                /*if(!$('#userType').val()){
                    $scope.alertModal({'content':'请选择用户审核类型'});
                    return;
                }*/
                
                if(!$('#isWork').val()){
                    $scope.alertModal({'content':'请选择是否当天排班'});
                    return;
                }
                
                $scope.record.userType = '1';

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.id ? $scope.app.project_name + '/forumPostCheck/update' : $scope.app.project_name + '/forumPostCheck/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                    	if (result.returnMsg != null && result.returnMsg != "") {
                    		$scope.alertModal({"content": result.returnMsg});
                    		$scope.isDisabled = false;
                    	} else {
                    		$scope.alertModal({"content": "操作成功"});
                            $timeout(function(){
                                $state.go('main.communityConfig.forumPostCheck.list');
                            },2000);
                    	}
                    }else{
                        $scope.alertModal({"content": "操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }
            
            function getUserList() {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/user/getUserList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.userList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}
            
            function getChannelList() {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/myChannel/getForumChannelList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.channelList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}
            
            // 初始化页面
            $scope.activate=function (id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/forumPostCheck/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        console.log(result.data.channelNo);
                        $scope.record.channelNo=result.data.channelNo;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            if ($state.includes('**.forumPostCheck.update')) {
                title = "修改审核人";
                var id = $state.params.id;
                getUserList();
                getChannelList();
                $scope.activate(id);

                $scope.isDisabled = false;
            } else if ($state.includes('**.forumPostCheck.add')) {
                title = "添加审核人";
                getUserList();
                getChannelList();
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
            
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
        }]);